package com.data.modules.smzh.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.constant.MsgConstants;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.common.system.vo.LoginUser;
import com.data.common.util.KamliClientUtil;
import com.data.modules.smzh.entity.SmComAnnouncement;
import com.data.modules.smzh.service.IBuildingService;
import com.data.modules.smzh.service.ISmComAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 小区公告信息
 * @Author: data-boot
 * @Date:   2023-01-04
 * @Version: V1.0
 */
@Api(tags="小区公告信息")
@RestController
@RequestMapping("/smzh/smComAnnouncement")
@Slf4j
public class SmComAnnouncementController extends BootController<SmComAnnouncement, ISmComAnnouncementService> {
	@Autowired
	private ISmComAnnouncementService smComAnnouncementService;

	@Autowired
	private IBuildingService buildingService;

	/**
	 * 分页列表查询
	 *
	 * @param smComAnnouncement
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "小区公告信息-分页列表查询")
	@ApiOperation(value="小区公告信息-分页列表查询", notes="小区公告信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SmComAnnouncement smComAnnouncement,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SmComAnnouncement> queryWrapper = QueryGenerator.initQueryWrapper(smComAnnouncement, req.getParameterMap());
		Page<SmComAnnouncement> page = new Page<SmComAnnouncement>(pageNo, pageSize);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if(ObjectUtil.notEqual(sysUser.getUsername(),"admin")){
			queryWrapper.eq("create_by",sysUser.getUsername());
		}
		IPage<SmComAnnouncement> pageList = smComAnnouncementService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param smComAnnouncement
	 * @return
	 */
	@AutoLog(value = "小区公告信息-添加")
	@ApiOperation(value="小区公告信息-添加", notes="小区公告信息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SmComAnnouncement smComAnnouncement) {

		/*if(ObjectUtil.contains(smComAnnouncement.getSerialNo(),",")){

			 Arrays.asList(smComAnnouncement.getSerialNo().split(",")).stream().forEach(
					x->{

						SmComAnnouncement sm = new SmComAnnouncement();
						sm.setSerialNo(x);
						sm.setImage(smComAnnouncement.getImage());
						smComAnnouncementService.save(sm);
					}
			);

		}*/
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if(ObjectUtil.contains(smComAnnouncement.getComNo(),",")){

			Arrays.asList(smComAnnouncement.getComNo().split(",")).stream().forEach(
					x->{

						SmComAnnouncement sm = new SmComAnnouncement();

						if(ObjectUtil.isNotEmpty(sysUser.getAppId())){
							sm.setAppId(sysUser.getAppId());
						}
						//sm.setSerialNo(x);
						sm.setComNo(x);
						sm.setImage(smComAnnouncement.getImage());
						smComAnnouncementService.save(sm);
					}
			);

		}else{
			if(ObjectUtil.isNotEmpty(sysUser.getAppId())){
				smComAnnouncement.setAppId(sysUser.getAppId());
			}
			smComAnnouncementService.save(smComAnnouncement);

		}

		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param smComAnnouncement
	 * @return
	 */
	@AutoLog(value = "小区公告信息-编辑")
	@ApiOperation(value="小区公告信息-编辑", notes="小区公告信息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SmComAnnouncement smComAnnouncement) {
		smComAnnouncementService.updateById(smComAnnouncement);
		return Result.OK("编辑成功!");
	}
	 @ApiOperation(value="推送小区公告信息", notes="推送小区公告信息")
	 @RequestMapping(value = "/push", method = RequestMethod.PUT)
	 public Result<?> push(@RequestBody JSONObject jsonObject) {


		 String id =  jsonObject.getString("id");
		 SmComAnnouncement announcement = smComAnnouncementService.getById(id);
		 if(ObjectUtil.isNotEmpty(announcement)){

		 	//通过项目主键查询出 绑定的 设备序列号推送消息
			 List<String> serialNoList =  buildingService.queryDeviceInfoByProjectId(announcement.getComNo());

			 serialNoList.parallelStream().forEach(x->{
				 List list = Arrays.asList(announcement.getImage().split(","));
				 Boolean flag=true;

				 flag = KamliClientUtil.doPostpushMsgUr(x, MsgConstants.DOOR_MSG,list);
				 log.info("门口机广告推送消息序列号{},结果：{}",x,flag);
			 });

		 }
		/* if(flag){
			 return Result.OK("推送成功!");
		 }else{
			 return Result.OK("推送失败!");
		 }*/
		 return Result.OK("推送成功!");

	 }


	@AutoLog(value = "批量推送小区公告信息")
	@ApiOperation(value="批量推送小区公告信息", notes="批量推送小区公告信息")
	@RequestMapping(value = "/batchPush", method = RequestMethod.PUT)
	public Result<?> batchPush(@RequestBody JSONObject jsonObject) {

		String ids =  jsonObject.getString("ids");
		List<String> list = Arrays.asList(ids.split(","));
		list.stream().forEach(x->{
			SmComAnnouncement announcement = smComAnnouncementService.getById(x);
			if(ObjectUtil.isNotEmpty(announcement)){

				//通过项目主键查询出 绑定的 设备序列号推送消息
				List<String> serialNoList =  buildingService.queryDeviceInfoByProjectId(announcement.getComNo());

				serialNoList.parallelStream().forEach(y->{
					List listImage = Arrays.asList(announcement.getImage().split(","));
					Boolean flag=true;
					flag = KamliClientUtil.doPostpushMsgUr(y,MsgConstants.DOOR_MSG,listImage);
					log.info("门口机广告推送消息序列号{},结果：{}",y,flag);
				});

				/*List listImage = Arrays.asList(announcement.getImage().split(","));

				KamliClientUtil.doPostpushMsgUr(announcement.getSerialNo(),"entDoorNotice",listImage);*/
			}
		});

		return Result.OK("推送成功!");
	}


	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "小区公告信息-通过id删除")
	@ApiOperation(value="小区公告信息-通过id删除", notes="小区公告信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		smComAnnouncementService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "小区公告信息-批量删除")
	@ApiOperation(value="小区公告信息-批量删除", notes="小区公告信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.smComAnnouncementService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "小区公告信息-通过id查询")
	@ApiOperation(value="小区公告信息-通过id查询", notes="小区公告信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SmComAnnouncement smComAnnouncement = smComAnnouncementService.getById(id);
		if(smComAnnouncement==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(smComAnnouncement);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param smComAnnouncement
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SmComAnnouncement smComAnnouncement) {
        return super.exportXls(request, smComAnnouncement, SmComAnnouncement.class, "小区公告信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SmComAnnouncement.class);
    }

}
